package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.customer.AddCustomerDTO;
import com.qk.dto.customer.CustomerDTO;
import com.qk.entity.Customer;
import com.qk.management.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/customers")
@Slf4j
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    
    @PutMapping("/{id}")
    public Result updateCustomer(@PathVariable Integer id, @RequestBody Customer customer){
        customer.setId(id);
        customerService.updateId(customer);
        return Result.success();
    }
    
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Customer customer = customerService.getById(id);
        return Result.success(customer);
    }

    @GetMapping
    public Result list(CustomerDTO dto){
        PageResult<Customer> pageResult = customerService.CCtlist(dto);
        return Result.success(pageResult);
    }

    @PutMapping
    public Result updateCustomer(@RequestBody Customer customer){
        customerService.updateId(customer);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody AddCustomerDTO dto){
        customerService.add(dto);
        return Result.success();
    }
}